package zj.boxuegu.administrator.myapplication.Fragment;

import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;

import zj.boxuegu.administrator.myapplication.Activity.CouserButtonActivity;
import zj.boxuegu.administrator.myapplication.Activity.CouserModel;
import zj.boxuegu.administrator.myapplication.R;

import static android.support.v7.recyclerview.R.styleable.RecyclerView;

/**
 * Created by Administrator on 2018/4/2.
 */

public class CouserFragment extends Fragment {
    RecyclerView recyclerView;

  ViewPager viewPager;

    MyViewHolder myViewholder;
    int curentPositon;

    public ArrayList<ImageView> viewPagerlist =new ArrayList<>();

   public ArrayList<CouserModel.Course>courselists =new ArrayList<>();
    Handler handler =new Handler(new Handler.Callback(){

        @Override
        public boolean handleMessage(Message message) {
            Log.i("CouserFragment","handleMessage"+message.what);
            handler.sendEmptyMessageDelayed(1,1000);
            curentPositon++;
            viewPager.setCurrentItem(curentPositon%viewPagerlist.size());

            return false;
        }
    });
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        couser1();

       View view=inflater.inflate(R.layout.fragment_couser, container, false);
        recyclerView =view.findViewById(R.id.RecyclerView);

        viewPager =view.findViewById(R.id.C_viewpager);
        myViewholder=new MyViewHolder(view);

        return view;

    }
//public void C_imgeView(View view){
//
//    }

    public  void couser1(){
        RequestParams params = new RequestParams("http://1633.applinzi.com/public/?s=App.Course.Getcourse");


        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {


                Gson gson = new Gson();

                CouserModel couserModel = gson.fromJson(result, CouserModel.class);
                if (couserModel!=null&&couserModel.data!=null&&couserModel.data.courselist!=null) {
                    courselists = couserModel.data.courselist;
//                if (couserModel.code == 200) {
//
//                }
                    // Log.i("LoginActivity","onSuccess loginModel code"+loginModel.code);

                }
                GridLayoutManager gridLayoutManager =new GridLayoutManager(getActivity(),2);
                recyclerView.setLayoutManager(gridLayoutManager);
                recyclerView.setAdapter(new MyAdatper());


                ArrayList<String>banner=couserModel.data.banner;
                  ImageView imageview1 =new ImageView(getActivity());
                x.image().bind(imageview1,banner.get(2));
            viewPagerlist.add(imageview1);
                for(int i =0;i<banner.size();i++){
                       ImageView imagview=new ImageView(getActivity());
                    x.image().bind(imagview,banner.get(i));
                   viewPagerlist.add(imagview);
                }
                  ImageView imageview2 =new ImageView(getActivity());
                x.image().bind(imageview2,banner.get(0));
                viewPagerlist.add(imageview2);
                    viewPager.setAdapter(new MyViewPagerAdatper());

                viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {

                                                      @Override
                                                      public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                                                      }

                                                      @Override
                                                      public void onPageSelected(int position) {
                                                          curentPositon = position;
                                                      }

                                                      @Override
                                                      public void onPageScrollStateChanged(int state) {
                                                       if(state!=ViewPager.SCROLL_STATE_IDLE){
                                                              return;
                                                       }
                                                       if(curentPositon==0){
                                                             viewPager.setCurrentItem(viewPagerlist.size()-2,false);
                                                       }else if(curentPositon==(viewPagerlist.size()-1)){
                                                                 viewPager.setCurrentItem(1,false);
                                                       }


                                                      }
                                                  }
                );

           viewPager.setCurrentItem(100);

                Log.i("CouserFragment","sendEmptyMessageDelayed");
                handler.sendEmptyMessageDelayed(1,2000);
                Log.i("LoginActivity","result"+result);



           }





            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Toast.makeText(x.app(), ex.getMessage(), Toast.LENGTH_LONG).show();


            }

            @Override
            public void onCancelled(CancelledException cex) {
                Toast.makeText(x.app(), "cancelled", Toast.LENGTH_LONG).show();
            }

            @Override
            public void onFinished() {

            }


        });
    }

    public  class MyAdatper extends  RecyclerView.Adapter<MyViewHolder>{

        @Override
        public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view =View.inflate(getActivity(),R.layout.item_couser,null);
            MyViewHolder myViewHolder =new MyViewHolder(view);

            return myViewHolder;
        }

        @Override
        public void onBindViewHolder(MyViewHolder holder, int position) {
            final CouserModel.Course course=courselists.get(position);
            holder.It_text1.setText(course.title);
            holder.It_text2.setText(course.titlecontent);
            x.image().bind(holder.It_img,course.icon);

            holder.CouserButton1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent =new Intent(getActivity(), CouserButtonActivity.class);
                    intent.putExtra("img",course.imgurl);
                   intent.putExtra("des",course.des);
                    intent.putExtra("video",course.videourl);
                    startActivity(intent);

                }
            });




        }





        @Override
        public int getItemCount() {
            return  courselists.size();

        }
    }

    public  class MyViewPagerAdatper extends PagerAdapter{

        @Override
        public int getCount() {
            return viewPagerlist.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
          //  return super.instantiateItem(container, position);

            ImageView imageView = viewPagerlist.get(position);
            container.addView(imageView);
            return imageView;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
           container.removeView(viewPagerlist.get(position));

        }
    }
    public  class MyViewHolder extends RecyclerView.ViewHolder{
        TextView It_text1,It_text2;
        ImageView It_img;
        LinearLayout CouserButton1;
        public MyViewHolder(View itemView) {
            super(itemView);
            It_text1=itemView.findViewById(R.id.it_text1);
            It_text2=itemView.findViewById(R.id.it_text2);
            It_img=itemView.findViewById(R.id.it_img);
            CouserButton1=itemView.findViewById(R.id.CouserButton1);

}
    }
}
